package com.nightpiter.view.drawer.rapid;

import java.util.ArrayList;

import com.nightpiter.model.objects.Station;
import com.nightpiter.model.objects.Transfer;
import com.nightpiter.model.objects.Tunnel;
import com.nightpiter.view.Calculator;
import com.nightpiter.view.drawer.ClassicMetroDrawer;
import com.nightpiter.view.drawer.Property;
import com.nightpiter.view.graphics.Canvas;
import com.nightpiter.view.graphics.Point;
import com.nightpiter.view.graphics.PointF;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public class RapidMetroDrawer extends ClassicMetroDrawer {

	/** a structure to draw */
	private Structure structure;

	/**
	 * default constructor
	 */
	public RapidMetroDrawer() {
	}

	private Property[] properties;
	
	private void setProperties() {
		Property properties[] = new Property[1];
		String value[] = {""};
		properties[0] = new Property("Corner Smooth", Property.Type.CHECKBOX, value);
		properties[0].setCurrentValue("true");
		this.setProperties(properties);
	}
	
	@Override
	public Property[] getProperties() {
		return this.properties;
	}
	
	@Override
	public void setProperties(Property[] newProp) {
		this.properties = newProp;
		//this.internalPrepare();
	}
	
	@Override
	protected boolean internalPrepare() {

		if (properties == null)
			setProperties();
		
		this.structure = new Structure(modelData, mode);

		return super.internalPrepare();
	}

	@Override
	public boolean update(Station station) {
		return this.structure.process(station);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.nightpiter.view.drawer.MetroDrawer#internalDrawMetro(com.nightpiter
	 * .view.graphics.Canvas, com.nightpiter.view.Calculator)
	 */
	@Override
	protected void internalDrawMetro(Canvas canvas, Calculator calculator) {
		Node.setup();
		LinkDrawer.setup();

		for (Node node : structure.getNodesMap().values()) {
			node.applyCalculator(calculator);
		}

		for (Link link : structure.getLinksMap().values()) {
			link.draw(canvas);
		}

		for (Node node : structure.getNodesMap().values()) {
			node.draw(canvas);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.nightpiter.view.drawer.MetroDrawer#selectStation(com.nightpiter.model
	 * .objects.Station, com.nightpiter.view.graphics.Canvas,
	 * com.nightpiter.view.Calculator)
	 */
	@Override
	public void selectStation(Station station, Canvas canvas,
			Calculator calculator) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.nightpiter.view.drawer.MetroDrawer#showWay(java.util.ArrayList,
	 * java.util.ArrayList, java.util.ArrayList,
	 * com.nightpiter.view.graphics.Canvas, com.nightpiter.view.Calculator)
	 */
	@Override
	public void showWay(ArrayList<Tunnel> selectedTunnels,
			ArrayList<Transfer> selectedTransfers,
			ArrayList<Station> selectedStations, Canvas canvas,
			Calculator calculator) {
		// TODO Auto-generated method stub

	}

	@Override
	public Station[] searchStation(Point point, Calculator calculator) {
		final int DELTA = 7;
		float minDist = Float.POSITIVE_INFINITY;
		float dist = 0;

		PointF pointF = new PointF();
		Node nearestNode = null;
		for (Node node : structure.getNodesMap().values()) {
			calculator.convert(node.getCoordF(), pointF);
			dist = pointF.distanceTo(point);
			if (dist < minDist) {
				minDist = dist;
				nearestNode = node;
			}
		}

		if (nearestNode != null && minDist < DELTA) {
			return (Station[]) nearestNode.getStations().toArray(
					new Station[nearestNode.getStations().size()]);
		}
		return new Station[0];
	}

}
